package net.oschina.dajiangnan.wx.util;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ipaynow1130 on 2017/4/19.
 */
public class Signature {


    public static String getSign(Object o, String[] excludeFields, String apiKey) throws IllegalAccessException {
        ArrayList<String> list = new ArrayList<String>();
        Class<? extends Object> cls = o.getClass();
        Field[] fields = cls.getDeclaredFields();
        for (Field f : fields) {
            f.setAccessible(true);
            if (f.get(o) != null && !f.get(o).equals("")) {
                if (excludeFields != null) {
                    boolean exclude = false;
                    for (String excludeField : excludeFields) {
                        if (f.getName().equals(excludeField)) {
                            exclude = true;
                            break;
                        }
                    }
                    if (!exclude) {
                        list.add(f.getName() + "=" + f.get(o) + "&");
                    }
                } else {
                    list.add(f.getName() + "=" + f.get(o) + "&");
                }


            }
        }
        int size = list.size();
        String[] arrayToSort = list.toArray(new String[size]);
        Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append(arrayToSort[i]);
        }
        String result = sb.toString();
        result += "key=" + apiKey;

        result = SecretUtil.ToMd5(result, "UTF-8",null).toUpperCase();
        return result;
    }


    public static String getSign(Map<String, Object> map, String apiKey) {
        ArrayList<String> list = new ArrayList<String>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (entry.getValue() != "") {
                list.add(entry.getKey() + "=" + entry.getValue() + "&");
            }
        }
        int size = list.size();
        String[] arrayToSort = list.toArray(new String[size]);
        Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append(arrayToSort[i]);
        }
        String result = sb.toString();

        result += "key=" + apiKey;

        result = SecretUtil.ToMd5(result, "UTF-8",null).toUpperCase();


        return result;
    }

    public static String getJsSign(Map<String, Object> map) {
        ArrayList<String> list = new ArrayList<String>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (entry.getValue() != "") {
                list.add(entry.getKey() + "=" + entry.getValue() + "&");
            }
        }
        int size = list.size();
        String[] arrayToSort = list.toArray(new String[size]);
        Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append(arrayToSort[i]);
        }
        String result = sb.toString();
        result = SecretUtil.Sha1Encode(result.substring(0, result.length() - 1));
        return result;
    }


    public static void main1(String[] args) throws IllegalAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("appid", "wx2e02e746d17d6b15");
        map.put("mch_id", "1250643401");
        map.put("nonce_str", "f6808210402125e30663234f94c87a8c");
        map.put("product_id", "8765");
        map.put("time_stamp", "1415949957");
        String apiKey = "511a793060fd11e5881728e347e0bee3";
        System.out.println(Signature.getSign(map, apiKey));
    }

    public static void main(String[] args) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("timestamp", String.valueOf("1414587457"));
        map.put("noncestr", "Wm3WZYTPz0wzccnW");
        map.put("jsapi_ticket", "sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg");
        map.put("url", "http://mp.weixin.qq.com?params=value");
        String signature = Signature.getJsSign(map);
        System.out.println(signature);
    }
}